package com.shambles.manage2.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.crypto.SecureUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shambles.manage2.service.SysUserService;
import com.shambles.manage2.tkmapper.entity.AbattoirBooth;
import com.shambles.manage2.tkmapper.entity.MessageRequestVM;
import com.shambles.manage2.tkmapper.entity.SysUser;
import com.shambles.manage2.tkmapper.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.weekend.Weekend;

/**
 * @author yangsy
 * @description
 * @date 2020/03/01
 */
@Service
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 查询商户
     */
    public PageInfo<SysUser> querUserAll(MessageRequestVM messageRequestVM,String post){
        return PageHelper.startPage(messageRequestVM.getPageIndex(), messageRequestVM.getPageSize()).doSelectPageInfo(() ->
                sysUserMapper.querUserAll(post)
        );
    }

    /**
     * 删除商户
     */
    public  void deleteSysUser(String guid){
        this.sysUserMapper.deleteSysUser(guid);
    }

    /**
     * 修改商户
     */
    public void updateSysUser(SysUser sysUser ){
        String Guid = sysUser.getGuid();
        Weekend<SysUser> sysUserWeekend = new Weekend<>(SysUser.class);
        sysUserWeekend.weekendCriteria().andEqualTo(SysUser::getGuid, Guid);

        this.sysUserMapper.updateByExampleSelective(sysUser, sysUserWeekend);

    }

    /**
     * 添加商户
     */
    public void insertSysUser(SysUser sysUser){
        SysUser insertSysUser=new SysUser();
        String userUuid = IdUtil.simpleUUID();
        String salt = SecureUtil.md5(userUuid);
        sysUser.setGuid(userUuid);
        insertSysUser.setGuid(IdUtil.simpleUUID());
        insertSysUser.setAccountName(sysUser.getAccountName());
        insertSysUser.setName(sysUser.getName());
        insertSysUser.setPhone(sysUser.getPhone());
        insertSysUser.setEmail(sysUser.getEmail());
        insertSysUser.setPassword(SecureUtil.md5(sysUser.getPassword()+salt));
        insertSysUser.setSalt(SecureUtil.md5(userUuid));
        insertSysUser.setCompanyGuid(IdUtil.simpleUUID());
        insertSysUser.setPost(sysUser.getPost());
        insertSysUser.setOpenid(sysUser.getOpenid());
        insertSysUser.setPeopleStatus(1);
        insertSysUser.setIdCard(sysUser.getIdCard());

      this.sysUserMapper.insertSelective(insertSysUser);
    }
}
